<?php
/**
 * 订单处理类
 */
namespace lib;
use think\Db;
use lib\Cart;
class OrderMen{
	public $data=[];
	protected $order_type;				//订单类型
	protected $pay_type;				//支付方式
	protected $ids;
	protected $user;
	protected $del=1;
	public $msg='';
	/**
	 * 初始化订单
	 * @param	$order_type		订单类型（参照配置文件base）
	 * @param	$pay_type		支付方式1：微信支付，2：余额支付
	 * @param	$ids			商品主键数组或单个字符串
	 * @param	$user			用户数据
	 */
	public function __construct($order_type,$pay_type,$ids,$user){
		$this->order_type=$order_type;
		$this->pay_type=$pay_type;
		if($order_type==1 && !is_array($ids)){
			$ids=explode(',', $ids);
		}
		$this->ids=$ids;
		$this->user=$user;
	}
	public function demo(){
		$this->del=0;
	}
	//获取订单数据
	public function get_order(){
		switch($this->order_type){
			case 1:	//购买普通商品
				$rel=$this->goods_order();
			break;
			case 2:	//购买专栏课程
				$rel=$this->subject_order();
			break;
			case 3:	//购买单栏
				$rel=$this->lesson_order();
			break;
			case 4:	//购买vip
				$rel=$this->vip_order();
			break;
			//其他订单类型待处理
		}
		if(!$rel){
			return false;	
		}else{
			return $this->data;
		}
	}
	//处理一般商品订单
	protected function goods_order(){
		$user	=$this->user;
		$ids	=$this->ids;							//商品主键数组
		$cart	=Cart::getCart();
		//取出购物车中商品主键和对应数量。第二个参数1代表直接删除购物车中对应商品
		$items=$cart->items($ids,$this->del);
		if(!empty($items)){
			$goods_price=0;								//商品原始总金额
			$ids=array_keys($items);
			$goods_arr=Db::name('goods')
			->field('goods_id as id,goods_name as name,price,img')
			->where(['goods_id'=>['in',$ids]])
			->select();
			array_walk($goods_arr,function(&$goods) use ($items,&$goods_price){
				$id=$goods['id'];							//商品主键
				$_num=$items[$id];					    	//该商品在购物车中的总数量
				$goods['num']=$_num;
				$goods['total_price']=$_num*$goods['price'];
				$goods_price+=$goods['total_price'];
			});
			$rel=$this->create_order('购买一般商品',$goods_price);
			if(!$rel){
				return false;
			}
			$order=$this->data;
			array_walk($goods_arr,function(&$goods) use ($order){
				$goods['order_id']=$order['order_id'];
				$goods['order_code']=$order['order_a'];		    //订单编号
			});
			Db::name('orderGoods')->insertAll($goods_arr);
			return true;
		}else{
			$this->msg='订单商品为空';
			return false;
		}
	}
	//处理专栏订单
	protected function subject_order(){
		$subject=Db::name('subject')->where(['id'=>$this->ids,'del'=>0])->find();
		if(!$subject){
			$this->msg='该课程已下架';
			return false;
		}
		return $this->create_order($subject['tit'],$subject['price'],$subject['id']);
	}
	//处理单栏订单
	protected function lesson_order(){
		$lesson=Db::name('lesson')->where(['id'=>$this->ids,'del'=>0])->find();
		if(!$lesson){
			$this->msg='该课程已下架';
			return false;
		}
		return $this->create_order($lesson['tit'],$lesson['price'],$lesson['id']);
	}
	//处理升级会员订单，ids为会员等级
	protected function vip_order(){
		$user=$this->user;
		$level=config('base.level');
		$level=$level[$this->ids];		//会员信息
		
		return $this->create_order($level['decl'],$level['price'],$this->ids);
	}
	/**
	 * 统一组装订单方法
	 * @param  $order_name	订单名称
	 * @param  $price		订单原始价格
	 * @param  $id			商品主键，例如专栏课程主键
	 * @param  $remark		订单备注信息
	 */
	protected function create_order($order_name,$price,$id=0,$remark=''){
		$user		=$this->user;
		$order_type	=$this->order_type;			//订单类型
		$pay_type	=$this->pay_type;			//支付方式
		$order_code	=order_code();				//本地订单号
		$time		=time();
		$price		=price($price);
		$order=[
			'id'		=> $id,
			'openid'	=> $user['openid'],
			'order_name'=> $order_name,
			'type' 		=> $order_type,
			'uid'  		=> $user['uid'],
			'name' 		=> !empty($user['real_name']) ? $user['real_name'] : $user['nickname'],
			'phone'		=> $user['phone'],
			'address'	=> !empty($user['address']) ? $user['address'] : '',
			'total_price'=>$price['total_price'],
			'goods_price'=>$price['score_price'],
			'rate'		=> $price['rate'],
			'rate_price'=> $price['rate_price'],
			'order_a'	=> $order_code,
			'remark'	=> $remark,
			'pay_type'  => $pay_type,
			'pay_status'=> 1,
			'ctime'		=> $time
		];
		$order_id=Db::name('order')->insertGetId($order);
		if(!$order_id){
			$this->msg='本地订单生成失败，稍后重试';
			return false;
		}else{
			$order['order_id']=$order_id;
			$this->data=$order;
			return true;
		}
	}
}
